<?php
namespace app\app\controller\v1;
use think\Db;
use app\app\util\OnlineUtils;
class IslandController extends  BaseController{

    /**
     * @return array
     * 首页热门岛屿
     */
    public function IslandHot()
    {
        $data = Db::name('island')->field('id,name,ename,cover')->limit(4)->order('id desc')->select();
        return json_code(200,'success',$data);
    }

    /**
     * @return array
     * 全部海岛列表
     */
    public function IslandList()
    {
        $pageindex = input('post.pageindex');
        $pagesize = input('post.pagesize');
        if( $pageindex <= 0 ){
            $length = 0;
        }else{
            $length = $pagesize*$pageindex;
        }
        $data = Db::name('island')->where(['status'=>1])->field('id,name,ename,feature,label')
            ->limit($length,$pagesize)->select();
        foreach ( $data as $a => $v){
            $labels = Db::name('island_label')->field('group_concat(name) name')->where('id','in',$v['label'])->find();
//            $label = explode(',',$v['label']);
//            for($i=0;$i<count($label);$i++){
//                $labe = Db::name('island_label')->field('name')->where('id','eq',$label[$i])->find();
//                $labels[] = $labe['name'];
//            }
            $data[$a]['label'] = explode(',',$labels['name']);
            //轮播第一张
            $data[$a]['slider']= Db::name('slider')->where(['island_id'=>$v['id'],'status'=>1])->field('id,img')->find();
            $data[$a]['slider'] =  $data[$a]['slider']['img'];
            $data[$a]['slider'] =  explode(',', $data[$a]['slider']);
            $data[$a]['slider'] = $data[$a]['slider'][0];
        }
        return json_code(200,'success',$data);
    }

    /**
     * @return array
     * 海岛详情
     */
    public function IslandDetail()
    {
        $id = input('get.id');
        $island = Db::name('island')->where(['status'=>1])->find($id);
        //海岛标签
        $labels = Db::name('island_label')->field('group_concat(name) name')->where('id','in',$island['label'])->find();
        $island['label'] = explode(',',$labels['name']);
        //轮播图
//        $slider = Db::name('slider')->where(['island_id'=>$island['id']])->field('img')->select();
//        $result = array_reduce($slider, function ($result, $value) {
//            return array_merge($result, array_values($value));
//        }, array());
        $slider = Db::name('slider')->where(['island_id'=>$id])->field('img')->find();
	
        $result =explode(',',$slider['img']);
		
        //岛屿视频
        $video = Db::name('island_video')->field('id,title,cover')->where(['island_id'=>$id,'status'=>1])->select();
        //酒店房型
        $hotel = Db::name('island_hotel')->field('id,cover,name,ename')->where(['island_id'=>$id,'status'=>1])->select();
        //餐厅美食
        $restaurant = Db::name('island_restaurant')->field('id,cover,name')->where(['island_id'=>$id,'status'=>1])->select();
        //娱乐项目
        $disport = Db::name('island_disport')->field('id,cover,name')->where(['island_id'=>$id,'status'=>1])->select();
        //下一站去哪
        $next_island = Db::name('island')->where('id','neq',$id)->field('id,name,ename,cover')->limit(5)->select();
        return json_code(200,'success',[
            'island'=>$island,
            'slider'=>$result,
            'video'=>$video,
            'hotel'=>$hotel,
            'restaurant'=>$restaurant,
            'disport'=>$disport,
            'next_island'=>$next_island
        ]);
    }

    /**
     * @return array
     * 岛屿视频详情
     */
    public function IslandVideoDetail()
    {
        $id = input('get.id');
        $data = Db::name('island_video')->where(['status'=>1])->find($id);
        return json_code(200,'success', $data);
    }

    /**
     * @return array
     * 酒店房型详情
     */
    public function IslandHotelDetail()
    {
        $id = input('get.id');
        $hotel = Db::name('island_hotel')->where(['status'=>1])->find($id);
        $slider = Db::name('slider')->where(['hotel_id'=>$id,'status'=>1])->field('id,img')->find();
        $slider['img'] = explode(',',$slider['img']);
        return json_code(200,'success', ['hotel'=>$hotel,'slider'=>$slider]);
   }

    /**
     * @return array
     * 岛屿美食餐厅详情
     */
    public function IslandRestaurantDetail()
    {
        $id = input('get.id');
        $restaurant = Db::name('island_restaurant')->where(['status'=>1])->find($id);
        $slider = Db::name('slider')->where(['restaurant_id'=>$id,'status'=>1])->field('id,img')->find();
        $slider['img'] = explode(',',$slider['img']);
        return json_code(200,'success', ['restaurant'=>$restaurant,'slider'=>$slider]);
   }

    /**
     * @return array
     * 娱乐项目详情
     */
    public function IslandDisportDetail()
    {
        $id = input('get.id');
        $disport = Db::name('island_disport')->where(['status'=>1])->find($id);
        $slider = Db::name('slider')->where(['disport_id'=>$id,'status'=>1])->field('id,img')->find();
        $slider['img'] = explode(',',$slider['img']);
        return json_code(200,'success', ['disport'=>$disport,'slider'=>$slider]);
   }


    /**
     * @return array
     * 下一站去哪
     */
    public function NextIsland()
    {
        
   }
}